/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Marcin
 */
public class Visit {
    public ResultSet getRoomDetail(int roomID)
    {
        try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
            "SELECT nazwa AS nazwa,opis AS opis FROM gabinety WHERE id=?",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
            stmt.setInt(1, roomID);
            stmt.executeQuery();
            return stmt.getResultSet();
        }
        catch(SQLException ex)
        {
            ErrorLogger.getInstance().log("Bład połączenia z bazą danych", true, null);
        }
        return null;  
    }
    
    public static ResultSet getVisitDetail(int visitID)
    {
        try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "SELECT rozpoznanie AS roz , zalecenia AS zal FROM historia_wizyt WHERE id=?");
            stmt.setInt(1, visitID);
            return stmt.executeQuery();
        }
        catch(SQLException ex)
        {
             ErrorLogger.getInstance().log(ex.toString(), true,null);
        } 
        return null;
    }
    
    public static void setVisitDetail(int visitID,String roz,String zal)
    {
         try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "UPDATE historia_wizyt SET rozpoznanie=? , zalecenia=? WHERE id=?");
            stmt.setInt(3, visitID);
            stmt.setString(1, roz);
            stmt.setString(2, zal);
            stmt.executeUpdate();
        }
        catch(SQLException ex)
        {
             ErrorLogger.getInstance().log(ex.toString(), true,null);
        } 
    }
    
    public static int getVisitId(int idPatient,int idMedic,String data)
    {
               try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "SELECT id AS ID FROM historia_wizyt WHERE id_pacjenta=? AND id_medyka=? AND data=CONVERT(DATE,?)");
            stmt.setInt(1, idPatient);
            stmt.setInt(2, idMedic);
            stmt.setString(3, data);
            ResultSet rs = stmt.executeQuery();
            rs.next();
            return rs.getInt("ID");
        }
        catch(SQLException ex)
        {
             ErrorLogger.getInstance().log(ex.toString(), true,null);
        } 
        return -1;
    }
    
    public static int getPatientID(Date from,Doctor d)
    {
        try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "SELECT id_pacjenta AS id_pacjenta "
                    + " FROM terminarz_wizyty_doktorskie "
                    + " WHERE id_medyka=? AND data_start=? ",
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmt.setInt(1, d.getID());
            stmt.setTimestamp(2, new java.sql.Timestamp(from.getTime()));
            stmt.executeQuery();
            ResultSet rs = stmt.getResultSet();
            if(rs.next()){
                return rs.getInt("id_pacjenta");
            }
            return -1;
        }
        catch(SQLException ex)
        {
            ErrorLogger.getInstance().log(ex.getMessage(), true, null);
        }
        return -1;
    }
}
